﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorkBasket.Data.Contracts;
using WorkBasket.Data.Entities;
using Microsoft.Owin;

namespace WorkBasket.Data.Repository
{
    public class SOC : IDisposable
    {

        private DbContext _Context;
 


        public void Dispose()
        {
            if (_Context != null)
            {
                _Context.Dispose();
                _Context = null;
            }
        }

 


        public SOC(DbContext context)
        {
            _Context = context;
            Fields =  SetRepository<Field>();
            Workflows = SetRepository<Workflow>();
            Steps = SetRepository<Step>();
            ProcessVersions = SetRepository <ProcessVersion>();
            Forms = SetRepository<Form>();
            FormControls = SetRepository<FormControl>();
            Instances = SetRepository<Instance>();
            InstanceHistorys = SetRepository<InstanceHistory>();
            Units = SetRepository<Unit>();
            Groups = SetRepository<Group>();
            GroupMembers = SetRepository<GroupMember>();
            GroupPropertys = SetRepository<GroupProperty>();
            Domains = SetRepository<Domain>();
            DomainUnits = SetRepository<DomainUnit>();
            Invites = SetRepository<Invite>();
            WBUsers = SetRepository<WBUser>();
            Settings = SetRepository<Setting>();
            Emails = SetRepository<Email>();
        }

        private RepositoryBase<T> SetRepository<T>() where T : class
        {
            return new RepositoryBase<T>(_Context);

        }
        public RepositoryBase<Setting> Settings { get; set; }
        public RepositoryBase<Email> Emails { get; set; }

        public RepositoryBase<WBUser> WBUsers { get; set; }
        public RepositoryBase<Invite> Invites { get; set; }

        public RepositoryBase<Domain> Domains { get; set; }
        public RepositoryBase<Field> Fields  { get; set; }
        public RepositoryBase<Workflow> Workflows { get; set; }

        public RepositoryBase<Step> Steps { get; set; }
        public RepositoryBase<ProcessVersion> ProcessVersions { get; set; }

        public RepositoryBase<Form> Forms { get; set; }
        public RepositoryBase<FormControl> FormControls { get; set; }
        public RepositoryBase<Instance> Instances { get; set; }
        public RepositoryBase<InstanceHistory> InstanceHistorys { get; set; }

        public RepositoryBase<Unit> Units { get; set; }
        public RepositoryBase<Group> Groups { get; set; }

        public RepositoryBase<GroupMember> GroupMembers { get; set; }
        public RepositoryBase<GroupProperty> GroupPropertys { get; set; }
        public RepositoryBase<DomainUnit> DomainUnits { get; set; }
   
        public void CommitChanges()
        {

            _Context.SaveChanges();
        }

 

        public IQueryable<T> Execute<T>(string procname, params SqlParameter[] arrParam) where T : class
        {
            return _Context.Database.SqlQuery<T>(procname, arrParam).AsQueryable<T>();


        }
 
    }
}
